package com.ruoyi.web.controller.eq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Catalog;
import com.ruoyi.system.mapper.CatalogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 地震事件
 * @author soulmate丶
 * @date 2021-07-08
 */
@RestController
@RequestMapping("/eq/catalog")
public class CatalogController extends BaseController {

    @Autowired
    private CatalogMapper catalogMapper;

    /**
     * 获取地震事件列表并分页
     * @param catalog
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(Catalog catalog)
    {
        startPage();
        System.out.println(catalog.getParams());
        if (catalog.getParams().get("beginTime") != null){
            QueryWrapper<Catalog> queryWrapper = new QueryWrapper<>();
            queryWrapper.ge("otime",catalog.getParams().get("beginTime"));
            queryWrapper.le("otime",catalog.getParams().get("endTime")+"23:59:59");
            List<Catalog> list = catalogMapper.selectList(queryWrapper);
            return getDataTable(list);
        }else {
            List<Catalog> list = catalogMapper.selectList(null);
            return getDataTable(list);
        }
    }


    /**
     * 获取一条地震信息详情
     * @param catalogId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = { "/", "/{catalogId}" })
    public AjaxResult getInfo(@PathVariable(value = "catalogId", required = false) String catalogId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(catalogId))
        {
            QueryWrapper<Catalog> queryWrapper = new QueryWrapper();
            queryWrapper.eq("scid",catalogId);
            ajax.put(AjaxResult.DATA_TAG, catalogMapper.selectOne(queryWrapper));
        }
        return ajax;
    }


    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "地震速报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Catalog catalog)
    {
        catalog.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(catalogMapper.updateById(catalog));
    }
}
